# -*- coding: utf-8 -*-
"""
Created on Sun Jul  2 15:25:12 2017

@author: claude
"""
import json
from  util import  OpenRequest
import os

   
def TokenGen():
    """
    APPKEY=xxx
    APPSEC=xxx
    export APPKEY
    export APPSEC
    """
    app_key = os.environ.get('APPKEY')
    app_sec = os.environ.get('APPSEC')
    global token
    token=FetchToken(app_key, app_sec)
    return token

    
def FetchToken(app_key, app_sec):
    url = "https://oauth.open.weidian.com/token?grant_type=client_credential&appkey="+app_key+"&secret="+app_sec
    content=OpenRequest.http_get(url)
    contentJson=json.loads(content,"utf8")
    if contentJson==None:
        return ""
    return contentJson['result']['access_token']
    